package com.ambow.store.service.impl;

import com.ambow.store.entity.DictDistrict;
import com.ambow.store.entity.User;
import com.ambow.store.mapper.DictDistrictMapper;
import com.ambow.store.service.DictDistrictService;
import com.ambow.store.service.exception.AddressTypeException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ambow.store.entity.Address;
import com.ambow.store.service.AddressService;
import com.ambow.store.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
* @author MyComputer
* @description 针对表【t_address】的数据库操作Service实现
* @createDate 2022-11-08 19:20:12
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService{

    @Value("${user.address.max-count}")
    private Integer counts;

    @Autowired
    private DictDistrictService districtService;

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void addUserAddress(Address address, HttpSession session) {
        //用lambda获取条件
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUid,session.getAttribute("uid"));
        //查询用户地址是否超过20条
        int count = this.count(addressLambdaQueryWrapper);
        if (count > counts){
            throw new AddressTypeException("收获地址数量超过20条！！！");
        }
        //对address对象中的数据进行补全
        address.setProvinceName(districtService.getNameByCode(address.getProvinceCode()).getName());
        address.setAreaName(districtService.getNameByCode(address.getAreaCode()).getName());
        address.setCityName(districtService.getNameByCode(address.getCityCode()).getName());

        address.setUid((Integer) session.getAttribute("uid"));
        address.setCreatedUser((String) session.getAttribute("username"));
        address.setCreatedTime(new Date());
        address.setModifiedUser((String) session.getAttribute("username"));
        address.setModifiedTime(new Date());
        address.setIsDefault(count == 0 ? 1 : 0);
        this.save(address);
    }

    @Override
    public List<Address> getAddressData(HttpSession session) {
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUid,session.getAttribute("uid"));
        addressLambdaQueryWrapper.orderByDesc(Address::getIsDefault);
        List<Address> list = this.list(addressLambdaQueryWrapper);
        return list;
    }

    @Override
    public void setDefaultAddress(Integer aid, Integer uid, String username) {
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getAid,aid);

        //查询数据库中是否有该地址
        Address result = this.getOne(addressLambdaQueryWrapper);
        if(result == null){
            throw new AddressTypeException("该地址异常或不存在");
        }
        if(!result.getUid().equals(uid)){
            throw new AddressTypeException("非法数据访问");
        }
        result.setModifiedTime(new Date());
        result.setModifiedUser(username);
        //修改
        Address address = new Address();
        address.setIsDefault(0);

        LambdaQueryWrapper<Address> setNonAddress = new LambdaQueryWrapper<>();
        setNonAddress.eq(Address::getUid,uid);
        this.update(address,setNonAddress);

        //设置指定aid的地址为默认地址
        result.setIsDefault(1);
        this.updateById(result);
    }

    @Override
    public void deleteAddress(Integer aid, Integer uid, String username) {
        LambdaQueryWrapper<Address> deleteLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deleteLambdaQueryWrapper.eq(Address::getAid,aid);
        //查询数据库中是否有该地址
        Address result = this.getOne(deleteLambdaQueryWrapper);
        if(result == null){
            throw new AddressTypeException("该地址异常或不存在");
        }
        if(!result.getUid().equals(uid)){
            throw new AddressTypeException("非法数据访问");
        }
        boolean remove = this.remove(deleteLambdaQueryWrapper);
        if( !remove){
            throw new AddressTypeException("删除地址是发生未知错误");
        }
        //查看该当前用户是否还有收获地址
        LambdaQueryWrapper<Address> count = new LambdaQueryWrapper<>();
        count.eq(Address::getUid,uid);
        List<Address> counts = this.list(count);
        //没有退出
        if(counts == null){
            return;
        }
        //不是默认地址退出
        if (result.getIsDefault() == 0){
            return;
        }
        //查询当前用户下的第一个地址的AID
        List<Object> objects = addressMapper.selectObjs(count);
        Integer setAid = (Integer) objects.get(0);

        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getAid,setAid);
        Address one = this.getOne(addressLambdaQueryWrapper);

        one.setIsDefault(1);
        one.setModifiedUser(username);
        one.setModifiedTime(new Date());

        this.updateById(one);

    }

    @Override
    public Address getByAid(Integer aid, Integer uid) {
        //根据收货地址的id，查询收货地址详情
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getAid,aid);
        Address one = this.getOne(addressLambdaQueryWrapper);

        if (one == null){
            throw new AddressTypeException("尝试访问的收货地址数据不存在");
        }
        if ( !one.getUid().equals(uid)){
            throw new AddressTypeException("非法访问");
        }

        return one;
    }
}




